package org.example.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.charset.StandardCharsets;

/**
 * @author hh
 * @since 2024/10/31
 */
public class TestHeapBuffer {
    public static void main(String[] args) {
        // 取得堆内存
        ByteBuf heapBuffer = ByteBufAllocator.DEFAULT.heapBuffer();
        heapBuffer.writeBytes("你好，世界".getBytes(StandardCharsets.UTF_8));
        if (heapBuffer.hasArray()) {
            // 取得内部数组
            byte[] array = heapBuffer.array();
            int offSet = heapBuffer.arrayOffset() + heapBuffer.readerIndex();
            int length = heapBuffer.readableBytes();
            System.out.println(new String(array, offSet, length, StandardCharsets.UTF_8));
        }
        heapBuffer.release();
    }
}
